﻿using System;
using Hongsh.Pay.Client.Proxy.Http;
using MapleLeaf.Pay;
using MapleLeaf.Pay.Pay;
using MapleLeaf.Pay.Trade;

namespace Hongsh.Pay.Client.Proxy.Handlers {
    // Token: 0x02000078 RID: 120
    public static class HeliPayProxy {
        // Token: 0x06000421 RID: 1057 RVA: 0x00004874 File Offset: 0x00002A74
        public static void AddHeliPayProxyHandler(this HttpServer svr) {
            svr.RouterList.Add("POST/trx/app/interface.action", new HttpContextActionDelegate(Process));
        }

        // Token: 0x06000422 RID: 1058 RVA: 0x0000FA94 File Offset: 0x0000DC94
        private static void Process(HttpContext ctx) {
            HeliPayResult heliPayResult = new HeliPayResult {
                rt1_bizType = ctx.Request.Form["P1_bizType"],
                rt5_orderId = ctx.Request.Form["P2_orderId"],
                rt6_serialNumber = ctx.Request.Form["P3_customerNumber"],
                rt2_retCode = "0000",
                rt15_channelRetCode = "0000"
            };
            try {
                HongshPayTrade hongshPayTrade = null;
                if (heliPayResult.rt1_bizType == "AppPay") {
                    hongshPayTrade = ctx.Execute(new TradeAuthPaymentRequest {
                        Amount = (int)Math.Floor(HongshExtends.ConvertTo<double>(ctx.Request.Form["P5_orderAmount"]) * 100.0),
                        Authcode = ctx.Request.Form["P7_authcode"],
                        ClientTradeId = heliPayResult.rt5_orderId
                    });
                }
                else if (heliPayResult.rt1_bizType == "AppPayQuery") {
                    hongshPayTrade = ctx.Execute(new TradePaymentQueryRequest {
                        ClientTradeId = heliPayResult.rt5_orderId
                    });
                }
                switch (hongshPayTrade.Payway) {
                    case TradePayway.微信:
                        heliPayResult.rt25_appPayType = "WXPAY";
                        break;
                    case TradePayway.支付宝:
                        heliPayResult.rt25_appPayType = "ALIPAY";
                        break;
                    case TradePayway.云闪付:
                        heliPayResult.rt25_appPayType = "UNIONPAY";
                        break;
                }
                if (hongshPayTrade.State == TradeState.失败) {
                    throw new Exception(hongshPayTrade.StateDescript);
                }
                heliPayResult.rt3_retMsg = hongshPayTrade.StateDescript;
                if (hongshPayTrade.State == TradeState.成功) {
                    heliPayResult.rt24_orderCompleteDate = hongshPayTrade.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss");
                    heliPayResult.rt16_outTransactionOrderId = hongshPayTrade.TradeId;
                    heliPayResult.rt13_orderStatus = "SUCCESS";
                    heliPayResult.rt3_retMsg = "支付成功！";
                }
                else {
                    heliPayResult.rt13_orderStatus = "DOING";
                    heliPayResult.rt15_channelRetCode = "USERPAYING";
                    if (string.IsNullOrEmpty(heliPayResult.rt3_retMsg)) {
                        heliPayResult.rt3_retMsg = "等待用户处理中！";
                    }
                }
                heliPayResult.rt10_orderAmount = (hongshPayTrade.Amount / 100m).ToString("0.00");
            }
            catch (Exception ex) {
                heliPayResult.rt13_orderStatus = "FAIL";
                heliPayResult.rt2_retCode = "0003";
                heliPayResult.rt15_channelRetCode = "AUTH_CODE_INVALID";
                heliPayResult.rt3_retMsg = ex.Message;
            }
            ctx.Result = new HttpJsonResult(heliPayResult);
        }

        // Token: 0x02000079 RID: 121
        public class HeliPayResult {
            // Token: 0x170001CD RID: 461
            // (get) Token: 0x06000423 RID: 1059 RVA: 0x00004892 File Offset: 0x00002A92
            // (set) Token: 0x06000424 RID: 1060 RVA: 0x0000489A File Offset: 0x00002A9A
            public string rt13_orderStatus { get; set; }

            // Token: 0x170001CE RID: 462
            // (get) Token: 0x06000425 RID: 1061 RVA: 0x000048A3 File Offset: 0x00002AA3
            // (set) Token: 0x06000426 RID: 1062 RVA: 0x000048AB File Offset: 0x00002AAB
            public string rt24_orderCompleteDate { get; set; }

            // Token: 0x170001CF RID: 463
            // (get) Token: 0x06000427 RID: 1063 RVA: 0x000048B4 File Offset: 0x00002AB4
            public string sign { get; }

            // Token: 0x170001D0 RID: 464
            // (get) Token: 0x06000428 RID: 1064 RVA: 0x000048BC File Offset: 0x00002ABC
            // (set) Token: 0x06000429 RID: 1065 RVA: 0x000048C4 File Offset: 0x00002AC4
            public string rt5_orderId { get; set; }

            // Token: 0x170001D1 RID: 465
            // (get) Token: 0x0600042A RID: 1066 RVA: 0x000048CD File Offset: 0x00002ACD
            // (set) Token: 0x0600042B RID: 1067 RVA: 0x000048D5 File Offset: 0x00002AD5
            public string rt7_payType { get; set; }

            // Token: 0x170001D2 RID: 466
            // (get) Token: 0x0600042C RID: 1068 RVA: 0x000048DE File Offset: 0x00002ADE
            // (set) Token: 0x0600042D RID: 1069 RVA: 0x000048E6 File Offset: 0x00002AE6
            public string tradeType { get; set; }

            // Token: 0x170001D3 RID: 467
            // (get) Token: 0x0600042E RID: 1070 RVA: 0x000048EF File Offset: 0x00002AEF
            public string productFee {
                get {
                    return rt10_orderAmount;
                }
            }

            // Token: 0x170001D4 RID: 468
            // (get) Token: 0x0600042F RID: 1071 RVA: 0x000048EF File Offset: 0x00002AEF
            public string channelSettlementAmount {
                get {
                    return rt10_orderAmount;
                }
            }

            // Token: 0x170001D5 RID: 469
            // (get) Token: 0x06000430 RID: 1072 RVA: 0x000048F7 File Offset: 0x00002AF7
            // (set) Token: 0x06000431 RID: 1073 RVA: 0x000048FF File Offset: 0x00002AFF
            public string rt6_serialNumber { get; set; }

            // Token: 0x170001D6 RID: 470
            // (get) Token: 0x06000432 RID: 1074 RVA: 0x00004908 File Offset: 0x00002B08
            // (set) Token: 0x06000433 RID: 1075 RVA: 0x00004910 File Offset: 0x00002B10
            public string rt11_currency { get; set; }

            // Token: 0x170001D7 RID: 471
            // (get) Token: 0x06000434 RID: 1076 RVA: 0x00004919 File Offset: 0x00002B19
            // (set) Token: 0x06000435 RID: 1077 RVA: 0x00004921 File Offset: 0x00002B21
            public string rt19_orderAttribute { get; set; }

            // Token: 0x170001D8 RID: 472
            // (get) Token: 0x06000436 RID: 1078 RVA: 0x0000492A File Offset: 0x00002B2A
            // (set) Token: 0x06000437 RID: 1079 RVA: 0x00004932 File Offset: 0x00002B32
            public string rt25_appPayType { get; set; }

            // Token: 0x170001D9 RID: 473
            // (get) Token: 0x06000438 RID: 1080 RVA: 0x0000493B File Offset: 0x00002B3B
            // (set) Token: 0x06000439 RID: 1081 RVA: 0x00004943 File Offset: 0x00002B43
            public string rt1_bizType { get; set; }

            // Token: 0x170001DA RID: 474
            // (get) Token: 0x0600043A RID: 1082 RVA: 0x0000494C File Offset: 0x00002B4C
            // (set) Token: 0x0600043B RID: 1083 RVA: 0x00004954 File Offset: 0x00002B54
            public string rt10_orderAmount { get; set; }

            // Token: 0x170001DB RID: 475
            // (get) Token: 0x0600043C RID: 1084 RVA: 0x000048EF File Offset: 0x00002AEF
            public string rt22_creditAmount {
                get {
                    return rt10_orderAmount;
                }
            }

            // Token: 0x170001DC RID: 476
            // (get) Token: 0x0600043D RID: 1085 RVA: 0x000048EF File Offset: 0x00002AEF
            public string rt23_paymentAmount {
                get {
                    return rt10_orderAmount;
                }
            }

            // Token: 0x170001DD RID: 477
            // (get) Token: 0x0600043E RID: 1086 RVA: 0x0000495D File Offset: 0x00002B5D
            public string realCreditAmount {
                get {
                    return rt23_paymentAmount;
                }
            }

            // Token: 0x170001DE RID: 478
            // (get) Token: 0x0600043F RID: 1087 RVA: 0x00004965 File Offset: 0x00002B65
            // (set) Token: 0x06000440 RID: 1088 RVA: 0x0000496D File Offset: 0x00002B6D
            public string rt2_retCode { get; set; }

            // Token: 0x170001DF RID: 479
            // (get) Token: 0x06000441 RID: 1089 RVA: 0x00004976 File Offset: 0x00002B76
            // (set) Token: 0x06000442 RID: 1090 RVA: 0x0000497E File Offset: 0x00002B7E
            public string rt16_outTransactionOrderId { get; set; }

            // Token: 0x170001E0 RID: 480
            // (get) Token: 0x06000443 RID: 1091 RVA: 0x00004987 File Offset: 0x00002B87
            // (set) Token: 0x06000444 RID: 1092 RVA: 0x0000498F File Offset: 0x00002B8F
            public string rt15_channelRetCode { get; set; }

            // Token: 0x170001E1 RID: 481
            // (get) Token: 0x06000445 RID: 1093 RVA: 0x00004998 File Offset: 0x00002B98
            // (set) Token: 0x06000446 RID: 1094 RVA: 0x000049A0 File Offset: 0x00002BA0
            public string rt3_retMsg { get; set; }

            // Token: 0x170001E2 RID: 482
            // (get) Token: 0x06000447 RID: 1095 RVA: 0x000049A9 File Offset: 0x00002BA9
            // (set) Token: 0x06000448 RID: 1096 RVA: 0x000049B1 File Offset: 0x00002BB1
            public string rt12_openId { get; set; }

            // Token: 0x170001E3 RID: 483
            // (get) Token: 0x06000449 RID: 1097 RVA: 0x000049BA File Offset: 0x00002BBA
            public string chargeFlag { get; }

            // Token: 0x170001E4 RID: 484
            // (get) Token: 0x0600044A RID: 1098 RVA: 0x000049C2 File Offset: 0x00002BC2
            public string resvData { get; }

            // Token: 0x170001E5 RID: 485
            // (get) Token: 0x0600044B RID: 1099 RVA: 0x000049CA File Offset: 0x00002BCA
            public string rt8_qrcode { get; }

            // Token: 0x170001E6 RID: 486
            // (get) Token: 0x0600044C RID: 1100 RVA: 0x000049D2 File Offset: 0x00002BD2
            public string upAddData { get; }

            // Token: 0x170001E7 RID: 487
            // (get) Token: 0x0600044D RID: 1101 RVA: 0x000049DA File Offset: 0x00002BDA
            public string ruleJson { get; }

            // Token: 0x0600044E RID: 1102 RVA: 0x0000FD1C File Offset: 0x0000DF1C
            public HeliPayResult() {
                //Class15.cwcZ3SWztHroi();
                sign = "e3051d8470c35f6ce826311031581bbf";
                rt7_payType = "SWIPE";
                tradeType = "MICROPAY";
                rt6_serialNumber = "1039807531";
                rt11_currency = "CNY";
                rt19_orderAttribute = "UNDIRECT_DEFAULT";
                rt12_openId = "unknow";
                chargeFlag = "";
                resvData = "";
                rt8_qrcode = "";
                upAddData = "";
                ruleJson = "";
                //base..ctor();
            }
        }
    }
}
